package com.qsmx.store.app.main.login;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.entity.login.MachineEntity;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.fustion.FustionPre;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.util.StringUtil;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;
import com.qsmx.store.core.util.loader.AtmosLoader;
import com.qsmx.store.core.util.storage.AtmosPreference;

import java.lang.reflect.Type;
import java.util.WeakHashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginMachineDelegate extends AtmosDelegate {

    private String machineCode;

    @BindView(R2.id.et_store_id)
    AppCompatEditText etStoreId;
    @BindView(R2.id.et_machine_id)
    AppCompatEditText etMachineId;

    @OnClick(R2.id.tv_sure)
    void onSure() {
        hideInput();
        if (StringUtil.isEmpty(etStoreId.getText().toString())) {
            showLongToast("请输入门店编号");
        } else if (StringUtil.isEmpty(etMachineId.getText().toString())) {
            showLongToast("请输入机台编号");
        } else {
            if (StringUtil.IntegerValueOf(etMachineId.getText().toString(), 0) < 10) {
                machineCode = etStoreId.getText().toString() + "-0" + etMachineId.getText().toString();
            } else {
                machineCode = etStoreId.getText().toString() + "-" + etMachineId.getText().toString();
            }
            WeakHashMap<String, Object> params = new WeakHashMap<>();
            params.put("machineCode", machineCode);
            HttpHelper.RestClientPost(params, HttpUrl.INIT_MACHINE, new ISuccess() {
                @Override
                public void onSuccess(int code, String response) {
                    AtmosPreference.addCustomStringPre(FustionPre.STORE_ID, etStoreId.getText().toString());
                    AtmosPreference.addCustomStringPre(FustionPre.MACHINE_ID, etMachineId.getText().toString());
                    Type type = new TypeToken<MachineEntity>() {
                    }.getType();
                    Gson gson = new Gson();
                    MachineEntity mData = gson.fromJson(response, type);
                    FustionManager.mMachineData = mData;
                    getSupportDelegate().start(new LoginDelegate());
                }
            }, new IError() {
                @Override
                public void onError(int code, String msg) {
                    showLongToast(msg);
                }
            }, new IFailure() {
                @Override
                public void onFailure() {

                }
            });
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_init_machine;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        AtmosLoader.showLoading(getContext());

        if (StringUtil.isNotEmpty(AtmosPreference.getCustomStringPre(FustionPre.STORE_ID))) {
            etStoreId.setText(AtmosPreference.getCustomStringPre(FustionPre.STORE_ID));
        }

        if (StringUtil.isNotEmpty(AtmosPreference.getCustomStringPre(FustionPre.MACHINE_ID))) {
            etMachineId.setText(AtmosPreference.getCustomStringPre(FustionPre.MACHINE_ID));
        }

        etStoreId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                hideInput();
            }
        });


        etMachineId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    showInput(etMachineId);
                } else {
                    hideInput();
                }
            }
        });
    }

    /**
     * 显示键盘
     *
     * @param et 输入焦点
     */
    public void showInput(final EditText et) {
        et.requestFocus();
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
    }

    /**
     * 隐藏键盘
     */
    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        View v = getProxyActivity().getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }
}
